package stack_queue.leetcode;

import java.util.Stack;

/**
 * 双栈模拟队列
 * @author yuisama
 * @date 2022/01/24 09:55
 **/
public class MyQueue {
    // 实际存储元素的栈
    private Stack<Integer> s1 = new Stack<>();
    // 辅助栈
    private Stack<Integer> s2 = new Stack<>();

    public MyQueue() {

    }

    public void push(int x) {
        if (s1.isEmpty()) {
            s1.push(x);
        }else {
            // 先把s1中的所有元素依次弹出放入s2
            while (!s1.isEmpty()) {
//                s2.push(s1.pop());
                int val = s1.pop();
                s2.push(val);
            }
            // 将新元素直接放入s1，此时新元素就处在s1的栈底
            s1.push(x);
            // 再次将s2的所有值依次弹出放入s1
            while (!s2.isEmpty()) {
                s1.push(s2.pop());
            }
        }
    }

    public int pop() {
        return s1.pop();
    }

    public int peek() {
        return s1.peek();
    }

    public boolean empty() {
        return s1.isEmpty();
    }
}